<template><div><h2 id="队列" tabindex="-1"><a class="header-anchor" href="#队列"><span>队列</span></a></h2>
<p>Yii2 Queue extension，An extension for running tasks asynchronously via queues. It supports queues based on <strong>DB</strong>, <strong>Redis</strong>, <strong>RabbitMQ</strong>, <strong>AMQP</strong>, <strong>Beanstalk</strong>, <strong>ActiveMQ</strong> and <strong>Gearman</strong>.</p>
<h2 id="安装" tabindex="-1"><a class="header-anchor" href="#安装"><span>安装</span></a></h2>
<p>安装此扩展的首选方式是通过 <a href="http://getcomposer.org/download/" target="_blank" rel="noopener noreferrer">composer</a>，命令如下：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require yiisoft/yii2-queue</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="配置" tabindex="-1"><a class="header-anchor" href="#配置"><span>配置</span></a></h2>
<p>为了使用扩展，你必须像下面这样配置它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// 组件注册它自己的控制台命令</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> \yii\queue\<span class="token operator">&lt;</span>driver<span class="token operator">></span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'as log'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>LogBehavior</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// 其他队列驱动程序选项</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>A list of available drivers and their docs is available in the <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/README.md" target="_blank" rel="noopener noreferrer">table of contents</a>.</p>
<h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<p>Each task which is sent to the queue should be defined as a separate class. For example, if you need to download and save a file the class may look like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">DownloadJob</span> <span class="token keyword">extends</span> <span class="token class-name">BaseObject</span> <span class="token keyword">implements</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>JobInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$url</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$file</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">execute</span><span class="token punctuation">(</span><span class="token variable">$queue</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">file_put_contents</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">file</span><span class="token punctuation">,</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">url</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Here’s how to send a task to the queue:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DownloadJob</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/tmp/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>To push a job into the queue that should run after 5 minutes:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token number">5</span> <span class="token operator">*</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DownloadJob</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/tmp/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>Important:</strong> Not all drivers support delayed running.</p>
<p>You can also specify a priority when pushing a job:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">priority</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ErrorNotification</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'recipient'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'notifyme@example.com'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Jobs with a lower priority will be executed first. The default priority is <code v-pre>1024</code>.</p>
<p><strong>Important:</strong> Not all drivers support job priorities.</p>
<h2 id="队列处理" tabindex="-1"><a class="header-anchor" href="#队列处理"><span>队列处理</span></a></h2>
<p>The exact way how a task is executed depends on the driver being used. Most drivers can be run using console commands, which the component registers in your application. For more details check the respective driver documentation.</p>
<h2 id="作业状态" tabindex="-1"><a class="header-anchor" href="#作业状态"><span>作业状态</span></a></h2>
<p>The component can track the status of a job that was pushed into the queue.</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Push a job into the queue and get a message ID.</span></span>
<span class="line"><span class="token variable">$id</span> <span class="token operator">=</span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SomeJob</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Check whether the job is waiting for execution.</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">isWaiting</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Check whether a worker got the job from the queue and executes it.</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">isReserved</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Check whether a worker has executed the job.</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">isDone</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><strong>Important:</strong> The RabbitMQ and AWS SQS drivers don’t support job statuses.</p>
<h2 id="messaging-third-party-workers" tabindex="-1"><a class="header-anchor" href="#messaging-third-party-workers"><span>Messaging third party workers</span></a></h2>
<p>You may pass any data to the queue:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'function'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'download'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/tmp/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>This is useful if the queue is processed using a custom third party worker.</p>
<p>If the worker is not implemented in PHP you have to change the way data is serialized. For example to serialize to JSON:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> \yii\queue\<span class="token operator">&lt;</span>driver<span class="token operator">></span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'strictJobType'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'serializer'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>serializers<span class="token punctuation">\</span>JsonSerializer</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="处理事件" tabindex="-1"><a class="header-anchor" href="#处理事件"><span>处理事件</span></a></h2>
<p>The queue triggers the following events:</p>
<table>
<thead>
<tr>
<th>Event name</th>
<th>Event class</th>
<th>Triggered</th>
</tr>
</thead>
<tbody>
<tr>
<td>Queue::EVENT_BEFORE_PUSH</td>
<td>PushEvent</td>
<td>before adding a job to queue using <code v-pre>Queue::push()</code> method</td>
</tr>
<tr>
<td>Queue::EVENT_AFTER_PUSH</td>
<td>PushEvent</td>
<td>after adding a job to queue using <code v-pre>Queue::push()</code> method</td>
</tr>
<tr>
<td>Queue::EVENT_BEFORE_EXEC</td>
<td>ExecEvent</td>
<td>before executing a job</td>
</tr>
<tr>
<td>Queue::EVENT_AFTER_EXEC</td>
<td>ExecEvent</td>
<td>after successful job execution</td>
</tr>
<tr>
<td>Queue::EVENT_AFTER_ERROR</td>
<td>ExecEvent</td>
<td>on uncaught exception during the job execution</td>
</tr>
<tr>
<td>cli\Queue:EVENT_WORKER_START</td>
<td>WorkerEvent</td>
<td>when worker has been started</td>
</tr>
<tr>
<td>cli\Queue:EVENT_WORKER_LOOP</td>
<td>WorkerEvent</td>
<td>on each iteration between requests to queue</td>
</tr>
<tr>
<td>cli\Queue:EVENT_WORKER_STOP</td>
<td>WorkerEvent</td>
<td>when worker has been stopped</td>
</tr>
</tbody>
</table>
<p>You can easily attach your own handler to any of these events. For example, let’s delay the job, if its execution failed with a special exception:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Queue</span><span class="token operator">::</span><span class="token constant">EVENT_AFTER_ERROR</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">error</span> <span class="token keyword">instanceof</span> <span class="token class-name">TemporaryUnprocessableJobException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$queue</span> <span class="token operator">=</span> <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">sender</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$queue</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token number">7200</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">job</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="日志记录事件" tabindex="-1"><a class="header-anchor" href="#日志记录事件"><span>日志记录事件</span></a></h2>
<p>The component provides the <code v-pre>LogBehavior</code> to log Queue events using <a href="http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html" target="_blank" rel="noopener noreferrer">Yii’s built-in Logger</a>.</p>
<p>To enable it, simply configure the queue component as follows:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'as log'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>LogBehavior</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="多个队列" tabindex="-1"><a class="header-anchor" href="#多个队列"><span>多个队列</span></a></h2>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue1'</span><span class="token punctuation">,</span> <span class="token comment">// First component registers its own console commands</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue2'</span><span class="token punctuation">,</span> <span class="token comment">// Second component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue1'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue2'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'strictJobType'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'serializer'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>serializers<span class="token punctuation">\</span>JsonSerializer</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Usage example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// Sending a task to the queue to be processed via standard worker</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue1</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">DownloadJob</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/tmp/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Sending a task to another queue to be processed by a third party worker</span></span>
<span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue2</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'function'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'download'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://example.com/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'file'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'/tmp/image.jpg'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="局限性" tabindex="-1"><a class="header-anchor" href="#局限性"><span>局限性</span></a></h2>
<p>When using queues it’s important to remember that tasks are put into and obtained from the queue in separate processes. Therefore avoid external dependencies when executing a task if you’re not sure if they are available in the environment where the worker does its job.</p>
<p>All the data to process the task should be put into properties of your job object and be sent into the queue along with it.</p>
<p>If you need to process an <code v-pre>ActiveRecord</code> then send its ID instead of the object itself. When processing you have to extract it from DB.</p>
<p>For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SomeJob</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'userId'</span> <span class="token operator">=></span> <span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'bookId'</span> <span class="token operator">=></span> <span class="token variable">$book</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'someUrl'</span> <span class="token operator">=></span> <span class="token class-name static-context">Url</span><span class="token operator">::</span><span class="token function">to</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'controller/action'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Task class:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SomeJob</span> <span class="token keyword">extends</span> <span class="token class-name">BaseObject</span> <span class="token keyword">implements</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>JobInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$userId</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$bookId</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token variable">$someUrl</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">execute</span><span class="token punctuation">(</span><span class="token variable">$queue</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">findOne</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">userId</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$book</span> <span class="token operator">=</span> <span class="token class-name static-context">Book</span><span class="token operator">::</span><span class="token function">findOne</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">bookId</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token comment">//...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="错误和可重试的作业" tabindex="-1"><a class="header-anchor" href="#错误和可重试的作业"><span>错误和可重试的作业</span></a></h2>
<p>The execution of a job can fail. This can be due to internal errors which result from poorly written code which should be fixed first. But they can also fail due to external problems like a service or a resource being unavailable. This can lead to exceptions or timeouts.</p>
<p>In the latter cases, it’s good to be able to retry a job after some time. There are several ways to do this.</p>
<blockquote>
<p><strong>Note:</strong> The <code v-pre>ttr</code> feature described below requires the <a href="http://php.net/manual/en/book.pcntl.php" target="_blank" rel="noopener noreferrer">PHP Process Control (pcntl) extension</a> to be installed and the worker command has to use the <code v-pre>--isolate</code> option (which is enabled by default).</p>
</blockquote>
<h3 id="retry-options" tabindex="-1"><a class="header-anchor" href="#retry-options"><span>Retry options</span></a></h3>
<p>The first method is to use queue component options:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> \yii\queue\<span class="token operator">&lt;</span>driver<span class="token operator">></span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'ttr'</span> <span class="token operator">=></span> <span class="token number">5</span> <span class="token operator">*</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token comment">// Max time for job execution</span></span>
<span class="line">        <span class="token string single-quoted-string">'attempts'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token comment">// Max number of attempts</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>The <code v-pre>ttr</code> (Time to reserve, TTR) option defines the number of seconds during which a job must be successfully completed. So two things can happen to make a job fail:</p>
<ol>
<li>The job throws an exception before <code v-pre>ttr</code> is over</li>
<li>It would take longer than <code v-pre>ttr</code> to complete the job (timeout) and thus the job execution is stopped by the worker.</li>
</ol>
<p>In both cases, the job will be sent back to the queue for a retry. Note though, that in the first case the <code v-pre>ttr</code> is still “used up” even if the job stops right after it has stared. I.e. the remaining seconds of <code v-pre>ttr</code> have to pass before the job is sent back to the queue.</p>
<p>The <code v-pre>attempts</code> option sets the max. number of attempts. If this number is reached, and the job still isn’t done, it will be removed from the queue as completed.</p>
<p>These options apply to all jobs in the queue. If you need to change this behavior for specific jobs, see the following method.</p>
<h3 id="retryablejobinterface" tabindex="-1"><a class="header-anchor" href="#retryablejobinterface"><span>RetryableJobInterface</span></a></h3>
<p>To have more control over the retry logic a job can implement the <code v-pre>RetryableJobInterface</code>. For example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">SomeJob</span> <span class="token keyword">extends</span> <span class="token class-name">BaseObject</span> <span class="token keyword">implements</span> <span class="token class-name">RetryableJobInterface</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">execute</span><span class="token punctuation">(</span><span class="token variable">$queue</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">//...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getTtr</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token number">15</span> <span class="token operator">*</span> <span class="token number">60</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">canRetry</span><span class="token punctuation">(</span><span class="token variable">$attempt</span><span class="token punctuation">,</span> <span class="token variable">$error</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token variable">$attempt</span> <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token variable">$error</span> <span class="token keyword">instanceof</span> <span class="token class-name">TemporaryException</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>The <code v-pre>getTtr()</code> and <code v-pre>canRetry()</code> methods have a higher priority than the component options mentioned above.</p>
<h3 id="事件处理器" tabindex="-1"><a class="header-anchor" href="#事件处理器"><span>事件处理器</span></a></h3>
<p>The third method to control TTR and number of retries for failed jobs involves the <code v-pre>Queue::EVENT_BEFORE_PUSH</code> and <code v-pre>Queue::EVENT_AFTER_ERROR</code> events.</p>
<p>The TTR can be set with the <code v-pre>Queue::EVENT_BEFORE_PUSH</code> event:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Queue</span><span class="token operator">::</span><span class="token constant">EVENT_BEFORE_PUSH</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">PushEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">job</span> <span class="token keyword">instanceof</span> <span class="token class-name">SomeJob</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">ttr</span> <span class="token operator">=</span> <span class="token number">300</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>And the <code v-pre>Queue::EVENT_AFTER_ERROR</code> event can be used to decide whether to try another attempt:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Yii</span><span class="token operator">::</span><span class="token variable">$app</span><span class="token operator">-></span><span class="token property">queue</span><span class="token operator">-></span><span class="token function">on</span><span class="token punctuation">(</span><span class="token class-name static-context">Queue</span><span class="token operator">::</span><span class="token constant">EVENT_AFTER_ERROR</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ExecEvent</span> <span class="token variable">$event</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">job</span> <span class="token keyword">instanceof</span> <span class="token class-name">SomeJob</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$event</span><span class="token operator">-></span><span class="token property">retry</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">attempt</span> <span class="token operator">&lt;</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token variable">$event</span><span class="token operator">-></span><span class="token property">error</span> <span class="token keyword">instanceof</span> <span class="token class-name">TemporaryException</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Event handlers are executed after the <code v-pre>RetryableJobInterface</code> methods, and therefore have the highest priority.</p>
<h3 id="限制条件" tabindex="-1"><a class="header-anchor" href="#限制条件"><span>限制条件</span></a></h3>
<p>Full support for retryable jobs is implemented in the <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-beanstalk.md" target="_blank" rel="noopener noreferrer">Beanstalk</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-db.md" target="_blank" rel="noopener noreferrer">DB</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-file.md" target="_blank" rel="noopener noreferrer">File</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-amqp-interop.md" target="_blank" rel="noopener noreferrer">AMQP Interop</a> and <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-redis.md" target="_blank" rel="noopener noreferrer">Redis</a> drivers. The <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-sync.md" target="_blank" rel="noopener noreferrer">Sync</a> driver will not retry failed jobs. The <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-gearman.md" target="_blank" rel="noopener noreferrer">Gearman</a> driver doesn’t support retryable jobs. <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-amqp.md" target="_blank" rel="noopener noreferrer">RabbitMQ</a> has only its basic retryable support, where the number of attempts can not be set.</p>
<p><a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-sqs.md" target="_blank" rel="noopener noreferrer">AWS SQS</a> uses <a href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html" target="_blank" rel="noopener noreferrer">Dead Letter Queue</a> for handling messages that were failed to process. All unprocessed messages after a maximum number of attempts are moved to that queue. You should set an address of a Dead Letter Queue and a maximum number of attempts in the AWS Console while creating a queue.</p>
<h2 id="启动-workers" tabindex="-1"><a class="header-anchor" href="#启动-workers"><span>启动 Workers</span></a></h2>
<h3 id="supervisor" tabindex="-1"><a class="header-anchor" href="#supervisor"><span>Supervisor</span></a></h3>
<p><a href="http://supervisord.org/" target="_blank" rel="noopener noreferrer">Supervisor</a> is a process monitor for Linux. It automatically starts console processes. On Ubuntu it can be installed with this command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> supervisor</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Supervisor config files are usually available in <code v-pre>/etc/supervisor/conf.d</code>. You can create any number of config files there.</p>
<p>Here’s an example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span>program<span class="token punctuation">:</span>yii<span class="token operator">-</span>queue<span class="token operator">-</span>worker<span class="token punctuation">]</span></span>
<span class="line">process_name<span class="token operator">=</span><span class="token operator">%</span><span class="token punctuation">(</span>program_name<span class="token punctuation">)</span>s_<span class="token operator">%</span><span class="token punctuation">(</span>process_num<span class="token punctuation">)</span><span class="token number">02</span>d</span>
<span class="line">command<span class="token operator">=</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>php <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>www<span class="token operator">/</span>my_project<span class="token operator">/</span>yii queue<span class="token operator">/</span>listen <span class="token operator">--</span>verbose<span class="token operator">=</span><span class="token number">1</span> <span class="token operator">--</span>color<span class="token operator">=</span><span class="token number">0</span></span>
<span class="line">autostart<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">autorestart<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">user<span class="token operator">=</span>www<span class="token operator">-</span>data</span>
<span class="line">numprocs<span class="token operator">=</span><span class="token number">4</span></span>
<span class="line">redirect_stderr<span class="token operator">=</span><span class="token constant boolean">true</span></span>
<span class="line">stdout_logfile<span class="token operator">=</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>www<span class="token operator">/</span>my_project<span class="token operator">/</span>log<span class="token operator">/</span>yii<span class="token operator">-</span>queue<span class="token operator">-</span>worker<span class="token operator">.</span>log</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>In this case Supervisor should start 4 <code v-pre>queue/listen</code> workers. The worker output will be written to the specified log file.</p>
<p>For more info about Supervisor’s configuration and usage see its <a href="http://supervisord.org/" target="_blank" rel="noopener noreferrer">documentation</a>.</p>
<p>Note that worker daemons started with <code v-pre>queue/listen</code> are only supported by the <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-file.md" target="_blank" rel="noopener noreferrer">File</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-db.md" target="_blank" rel="noopener noreferrer">Db</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-redis.md" target="_blank" rel="noopener noreferrer">Redis</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-amqp.md" target="_blank" rel="noopener noreferrer">RabbitMQ</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-amqp-interop.md" target="_blank" rel="noopener noreferrer">AMQP Interop</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-beanstalk.md" target="_blank" rel="noopener noreferrer">Beanstalk</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-gearman.md" target="_blank" rel="noopener noreferrer">Gearman</a> and <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-sqs.md" target="_blank" rel="noopener noreferrer">AWS SQS</a> drivers. For additional options see driver guide.</p>
<h3 id="systemd" tabindex="-1"><a class="header-anchor" href="#systemd"><span>Systemd</span></a></h3>
<p>Systemd is another init system used on Linux to bootstrap the user space. To configure workers startup using systemd, create a config file named <code v-pre>yii-queue@.service</code> in <code v-pre>/etc/systemd/system</code> with the following content:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">[</span>Unit<span class="token punctuation">]</span></span>
<span class="line">Description<span class="token operator">=</span>Yii Queue Worker <span class="token operator">%</span><span class="token constant">I</span></span>
<span class="line">After<span class="token operator">=</span>network<span class="token operator">.</span>target</span>
<span class="line"><span class="token comment"># the following two lines only apply if your queue backend is mysql</span></span>
<span class="line"><span class="token comment"># replace this with the service that powers your backend</span></span>
<span class="line">After<span class="token operator">=</span>mysql<span class="token operator">.</span>service</span>
<span class="line">Requires<span class="token operator">=</span>mysql<span class="token operator">.</span>service</span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span>Service<span class="token punctuation">]</span></span>
<span class="line">User<span class="token operator">=</span>www<span class="token operator">-</span>data</span>
<span class="line">Group<span class="token operator">=</span>www<span class="token operator">-</span>data</span>
<span class="line">ExecStart<span class="token operator">=</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>php <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>www<span class="token operator">/</span>my_project<span class="token operator">/</span>yii queue<span class="token operator">/</span>listen <span class="token operator">--</span>verbose</span>
<span class="line">Restart<span class="token operator">=</span>on<span class="token operator">-</span>failure</span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span>Install<span class="token punctuation">]</span></span>
<span class="line">WantedBy<span class="token operator">=</span>multi<span class="token operator">-</span>user<span class="token operator">.</span>target</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You need to reload systemd in order to re-read its configuration:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">systemctl daemon-reload</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Set of commands to control workers:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># To start two workers</span></span>
<span class="line">systemctl start yii-queue@1 yii-queue@2</span>
<span class="line"></span>
<span class="line"><span class="token comment"># To get the status of running workers</span></span>
<span class="line">systemctl status <span class="token string">"yii-queue@*"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># To stop a specific worker</span></span>
<span class="line">systemctl stop yii-queue@2</span>
<span class="line"></span>
<span class="line"><span class="token comment"># To stop all running workers</span></span>
<span class="line">systemctl stop <span class="token string">"yii-queue@*"</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># To start two workers at system boot</span></span>
<span class="line">systemctl <span class="token builtin class-name">enable</span> yii-queue@1 yii-queue@2</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>To learn all features of systemd, check its <a href="https://freedesktop.org/wiki/Software/systemd/#manualsanddocumentationforusersandadministrators" target="_blank" rel="noopener noreferrer">documentation</a>.</p>
<h3 id="cron" tabindex="-1"><a class="header-anchor" href="#cron"><span>Cron</span></a></h3>
<p>You can also start workers using cron. Here you have to use the <code v-pre>queue/run</code> command.</p>
<p>Config example:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">* * * * * /usr/bin/php /var/www/my_project/yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>In this case cron will run the command every minute.</p>
<p>The <code v-pre>queue/run</code> command is supported by the <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-file.md" target="_blank" rel="noopener noreferrer">File</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-db.md" target="_blank" rel="noopener noreferrer">Db</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-redis.md" target="_blank" rel="noopener noreferrer">Redis</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-beanstalk.md" target="_blank" rel="noopener noreferrer">Beanstalk</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-gearman.md" target="_blank" rel="noopener noreferrer">Gearman</a>, <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-sqs.md" target="_blank" rel="noopener noreferrer">AWS SQS</a> drivers. For additional options see driver guide.</p>
<h2 id="队列驱动" tabindex="-1"><a class="header-anchor" href="#队列驱动"><span>队列驱动</span></a></h2>
<h3 id="synchronous-driver" tabindex="-1"><a class="header-anchor" href="#synchronous-driver"><span>Synchronous Driver</span></a></h3>
<p>Runs tasks synchronously in the same process if the <code v-pre>handle</code> property is turned on. It could be used when developing and debugging an application.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>sync<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'handle'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span> <span class="token comment">// whether tasks should be executed immediately</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="file-driver" tabindex="-1"><a class="header-anchor" href="#file-driver"><span>File Driver</span></a></h3>
<p>The file driver uses files to store queue data.</p>
<p>Configuration example:</p>
<div class="language-text-html-php line-numbers-mode" data-highlighter="prismjs" data-ext="text-html-php" data-title="text-html-php"><pre v-pre class="language-text-html-php"><code><span class="line">return [</span>
<span class="line">    'bootstrap' =&gt; [</span>
<span class="line">        'queue', // The component registers its own console commands</span>
<span class="line">    ],</span>
<span class="line">    'components' =&gt; [</span>
<span class="line">        'queue' =&gt; [</span>
<span class="line">            'class' =&gt; \yii\queue\file\Queue::class,</span>
<span class="line">            'path' =&gt; '@runtime/queue',</span>
<span class="line">        ],</span>
<span class="line">    ],</span>
<span class="line">];</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console" tabindex="-1"><a class="header-anchor" href="#console"><span>Console</span></a></h4>
<p>Console commands are used to execute and manage queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. The <code v-pre>timeout</code> parameter specifies the number of seconds to sleep between querying the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>run</code> command obtains and executes tasks in a loop until the queue is empty. This works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p>The <code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print execution statuses to console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: enable highlighting for verbose mode.</li>
</ul>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/info</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>info</code> command prints out information about the queue status.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/clear</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>clear</code> command clears the queue.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/remove <span class="token punctuation">[</span>id<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>remove</code> command removes a job from the queue.</p>
<h3 id="db-driver" tabindex="-1"><a class="header-anchor" href="#db-driver"><span>DB Driver</span></a></h3>
<p>The DB driver uses a database to store queue data.</p>
<p>It supports:</p>
<ul>
<li>priorities</li>
<li>delays</li>
<li>ttr</li>
<li>attempts</li>
</ul>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Connection</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> </span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>db<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'db'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'db'</span><span class="token punctuation">,</span> <span class="token comment">// DB connection component or its config </span></span>
<span class="line">            <span class="token string single-quoted-string">'tableName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'{{%queue}}'</span><span class="token punctuation">,</span> <span class="token comment">// Table name</span></span>
<span class="line">            <span class="token string single-quoted-string">'channel'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">,</span> <span class="token comment">// Queue channel key</span></span>
<span class="line">            <span class="token string single-quoted-string">'mutex'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>mutex<span class="token punctuation">\</span>MysqlMutex</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token comment">// Mutex used to sync queries</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You have to add a table to the database. Example schema for:</p>
<p>MySQL:</p>
<div class="language-sql line-numbers-mode" data-highlighter="prismjs" data-ext="sql" data-title="sql"><pre v-pre class="language-sql"><code><span class="line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token identifier"><span class="token punctuation">`</span>queue<span class="token punctuation">`</span></span> <span class="token punctuation">(</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>channel<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>job<span class="token punctuation">`</span></span> <span class="token keyword">longblob</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>pushed_at<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>ttr<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>delay<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>priority<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">unsigned</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token number">1024</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>reserved_at<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>attempt<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token identifier"><span class="token punctuation">`</span>done_at<span class="token punctuation">`</span></span> <span class="token keyword">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token keyword">KEY</span> <span class="token identifier"><span class="token punctuation">`</span>channel<span class="token punctuation">`</span></span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>channel<span class="token punctuation">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token keyword">KEY</span> <span class="token identifier"><span class="token punctuation">`</span>reserved_at<span class="token punctuation">`</span></span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>reserved_at<span class="token punctuation">`</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token keyword">KEY</span> <span class="token identifier"><span class="token punctuation">`</span>priority<span class="token punctuation">`</span></span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>priority<span class="token punctuation">`</span></span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>and Postgresql</p>
<div class="language-sql line-numbers-mode" data-highlighter="prismjs" data-ext="sql" data-title="sql"><pre v-pre class="language-sql"><code><span class="line"><span class="token comment">-- Necessary for creating Autoincrement field</span></span>
<span class="line"><span class="token keyword">CREATE</span> SEQUENCE queue_seq<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> queue <span class="token punctuation">(</span></span>
<span class="line">  id <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> NEXTVAL <span class="token punctuation">(</span><span class="token string">'queue_seq'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">  channel <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  job bytea <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  pushed_at <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  ttr <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span><span class="token punctuation">,</span></span>
<span class="line">  delay <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token number">0</span><span class="token punctuation">,</span></span>
<span class="line">  priority <span class="token keyword">bigint</span> <span class="token keyword">check</span> <span class="token punctuation">(</span>priority <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">DEFAULT</span> <span class="token number">1024</span><span class="token punctuation">,</span></span>
<span class="line">  reserved_at <span class="token keyword">bigint</span> <span class="token punctuation">,</span></span>
<span class="line">  attempt <span class="token keyword">bigint</span><span class="token punctuation">,</span></span>
<span class="line">  done_at <span class="token keyword">bigint</span><span class="token punctuation">,</span></span>
<span class="line">  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>id<span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">-- Optional but good for speeding up queries</span></span>
<span class="line"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> channel <span class="token keyword">ON</span> queue <span class="token punctuation">(</span>channel<span class="token punctuation">)</span><span class="token punctuation">;</span> </span>
<span class="line"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> reserved_at <span class="token keyword">ON</span> queue <span class="token punctuation">(</span>reserved_at<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> priority <span class="token keyword">ON</span> queue <span class="token punctuation">(</span>priority<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>You can use migrations which are available from <a href="https://github.com/yiisoft/yii2-queue/blob/master/src/drivers/db/migrations" target="_blank" rel="noopener noreferrer">src/drivers/db/migrations</a>.</p>
<p>To add migrations to your application, edit the console config file to configure <a href="http://www.yiiframework.com/doc-2.0/guide-db-migrations.html#namespaced-migrations" target="_blank" rel="noopener noreferrer">a namespaced migration</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'controllerMap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line">    <span class="token string single-quoted-string">'migrate'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\console\controllers\MigrateController'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrationPath'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'migrationNamespaces'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line">            <span class="token string single-quoted-string">'yii\queue\db\migrations'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Then issue the <code v-pre>migrate/up</code> command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii migrate/up</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="console-1" tabindex="-1"><a class="header-anchor" href="#console-1"><span>Console</span></a></h4>
<p>Console commands are used to execute and manage queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. The <code v-pre>timeout</code> parameter specifies the number of seconds to sleep between querying the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>run</code> command obtains and executes tasks in a loop until the queue is empty. This works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p>The <code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print execution statuses to console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: enable highlighting for verbose mode.</li>
</ul>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/info</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>info</code> command prints out information about the queue status.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/clear</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>clear</code> command clears the queue.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/remove <span class="token punctuation">[</span>id<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>remove</code> command removes a job from the queue.</p>
<h3 id="redis-driver" tabindex="-1"><a class="header-anchor" href="#redis-driver"><span>Redis Driver</span></a></h3>
<p>This driver uses Redis to store queue data.</p>
<p>You have to add the <code v-pre>yiisoft/yii2-redis</code> extension to your application in order to use it.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Connection</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// retry connecting after connection has timed out</span></span>
<span class="line">            <span class="token comment">// yiisoft/yii2-redis >=2.0.7 is required for this.</span></span>
<span class="line">            <span class="token string single-quoted-string">'retries'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>redis<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'redis'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span> <span class="token comment">// Redis connection component or its config</span></span>
<span class="line">            <span class="token string single-quoted-string">'channel'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// Queue channel key</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-2" tabindex="-1"><a class="header-anchor" href="#console-2"><span>Console</span></a></h4>
<p>Console commands are used to execute and manage queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. The <code v-pre>timeout</code> parameter specifies the number of seconds to sleep between querying the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>run</code> command obtains and executes tasks in a loop until the queue is empty. This works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p>The <code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print execution statuses to console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: enable highlighting for verbose mode.</li>
</ul>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/info</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>info</code> command prints out information about the queue status.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/clear</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>clear</code> command clears the queue.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/remove <span class="token punctuation">[</span>id<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>remove</code> command removes a job from the queue.</p>
<h3 id="rabbitmq-driver" tabindex="-1"><a class="header-anchor" href="#rabbitmq-driver"><span>RabbitMQ Driver</span></a></h3>
<p><strong>Note:</strong> This driver has been deprecated since 2.0.2 and will be removed in 2.1. Consider using the <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/driver-amqp-interop.md" target="_blank" rel="noopener noreferrer">amqp_interop</a> driver instead.</p>
<p>This driver works with RabbitMQ queues.</p>
<p>It requires the <code v-pre>php-amqplib/php-amqplib</code> package.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>amqp<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">5672</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'queueName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-3" tabindex="-1"><a class="header-anchor" href="#console-3"><span>Console</span></a></h4>
<p>A console command is used to execute queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<h3 id="amqp-interop" tabindex="-1"><a class="header-anchor" href="#amqp-interop"><span>AMQP Interop</span></a></h3>
<p>This driver works with RabbitMQ queues.</p>
<p>It requires an <a href="https://github.com/queue-interop/queue-interop#amqp-interop" target="_blank" rel="noopener noreferrer">amqp interop</a> compatible transport, for example the <code v-pre>enqueue/amqp-lib</code> package.</p>
<p>Advantages:</p>
<ul>
<li>
<p>It works with any amqp interop compatible transports, such as</p>
<ul>
<li><a href="https://github.com/php-enqueue/amqp-ext" target="_blank" rel="noopener noreferrer">enqueue/amqp-ext</a> based on <a href="https://github.com/pdezwart/php-amqp" target="_blank" rel="noopener noreferrer">PHP amqp extension</a></li>
<li><a href="https://github.com/php-enqueue/amqp-lib" target="_blank" rel="noopener noreferrer">enqueue/amqp-lib</a> based on <a href="https://github.com/php-amqplib/php-amqplib" target="_blank" rel="noopener noreferrer">php-amqplib/php-amqplib</a></li>
<li><a href="https://github.com/php-enqueue/amqp-bunny" target="_blank" rel="noopener noreferrer">enqueue/amqp-bunny</a> based on <a href="https://github.com/jakubkulhan/bunny" target="_blank" rel="noopener noreferrer">bunny</a></li>
</ul>
</li>
<li>
<p>Supports priorities</p>
</li>
<li>
<p>Supports delays</p>
</li>
<li>
<p>Supports ttr</p>
</li>
<li>
<p>Supports attempts</p>
</li>
<li>
<p>Contains new options like: vhost, connection_timeout, qos_prefetch_count and so on.</p>
</li>
<li>
<p>Supports Secure (SSL) AMQP connections.</p>
</li>
<li>
<p>Has the ability to set DSN like: amqp:, amqps: or amqp://user:pass@localhost:1000/vhost</p>
</li>
</ul>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>amqp_interop<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">5672</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'guest'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'queueName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'driver'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context">yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>amqp_interop<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token constant">ENQUEUE_AMQP_LIB</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// or</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'amqp://guest:guest@localhost:5672/%2F'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">            <span class="token comment">// or, same as above</span></span>
<span class="line">            <span class="token string single-quoted-string">'dsn'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'amqp:'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-4" tabindex="-1"><a class="header-anchor" href="#console-4"><span>Console</span></a></h4>
<p>A console command is used to execute queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<h3 id="beanstalk-driver" tabindex="-1"><a class="header-anchor" href="#beanstalk-driver"><span>Beanstalk Driver</span></a></h3>
<p>This driver works with Beanstalk queues.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>beanstalk<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">11300</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'tube'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-5" tabindex="-1"><a class="header-anchor" href="#console-5"><span>Console</span></a></h4>
<p>Console commands are used to execute and manage queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. The <code v-pre>timeout</code> parameter specifies the number of seconds to sleep between querying the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>run</code> command obtains and executes tasks in a loop until the queue is empty. This works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p>The <code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print execution status to console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: enable highlighting for verbose mode.</li>
</ul>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/info</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>info</code> command prints out information about the queue status.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/remove <span class="token punctuation">[</span>id<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>remove</code> command removes a job from the queue.</p>
<h3 id="gearman-driver" tabindex="-1"><a class="header-anchor" href="#gearman-driver"><span>Gearman Driver</span></a></h3>
<p>This driver works with Gearman queues.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>gearman<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">4730</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'channel'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'my_queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-6" tabindex="-1"><a class="header-anchor" href="#console-6"><span>Console</span></a></h4>
<p>Console commands are used to process queued tasks.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. This method is most efficient when command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>run</code> command obtains and executes tasks in a loop until queue is empty. Works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p><code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print executing statuses into console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: highlighting for verbose mode.</li>
</ul>
<h3 id="aws-sqs-driver" tabindex="-1"><a class="header-anchor" href="#aws-sqs-driver"><span>AWS SQS Driver</span></a></h3>
<p>The driver uses AWS SQS to store queue data.</p>
<p>You have to add <code v-pre>aws/aws-sdk-php</code> extension to your application in order to use it.</p>
<p>Configuration example for standard queues:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>sqs<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;sqs url>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;key>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;secret>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;region>'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Configuration example for FIFO queues:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>sqs<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;sqs url>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;key>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'secret'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;secret>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'region'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;region>'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'messageGroupId'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'&lt;Group ID>'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>The message group ID is required by SQS for FIFO queues. You can configure your own or use the “default” value.</p>
<p>The deduplication ID is generated automatically, so no matter if you have activated content-based deduplication in the SQS queue or not, this ID will be used.</p>
<h4 id="console-7" tabindex="-1"><a class="header-anchor" href="#console-7"><span>Console</span></a></h4>
<p>Console command is used to execute tasks.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. <code v-pre>timeout</code> parameter is number of seconds to wait a job. It uses SQS “Long Polling” feature, that holds a connection between client and a queue.</p>
<p><strong>Important:</strong> <code v-pre>timeout</code> parameter for SQS driver must be in range between 0 and 20 seconds.</p>
<p>This method is most efficient when command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/run</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>run</code> command obtains and executes tasks in a loop until queue is empty. Works well with <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#cron" target="_blank" rel="noopener noreferrer">cron</a>.</p>
<p><code v-pre>run</code> and <code v-pre>listen</code> commands have options:</p>
<ul>
<li><code v-pre>--verbose</code>, <code v-pre>-v</code>: print executing statuses into console.</li>
<li><code v-pre>--isolate</code>: each task is executed in a separate child process.</li>
<li><code v-pre>--color</code>: highlighting for verbose mode.</li>
</ul>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/clear</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>clear</code> command clears a queue.</p>
<h3 id="stomp-driver" tabindex="-1"><a class="header-anchor" href="#stomp-driver"><span>Stomp Driver</span></a></h3>
<p>This driver works with ActiveMQ queues.</p>
<p>It requires the <code v-pre>enqueue/stomp</code> package.</p>
<p>Configuration example:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'bootstrap'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span> <span class="token comment">// The component registers its own console commands</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'components'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>stomp<span class="token punctuation">\</span>Queue</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'host'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'localhost'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'port'</span> <span class="token operator">=></span> <span class="token number">61613</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'queueName'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="console-8" tabindex="-1"><a class="header-anchor" href="#console-8"><span>Console</span></a></h4>
<p>A console command is used to execute queued jobs.</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">yii queue/listen <span class="token punctuation">[</span>timeout<span class="token punctuation">]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>The <code v-pre>listen</code> command launches a daemon which infinitely queries the queue. If there are new tasks they’re immediately obtained and executed. The <code v-pre>timeout</code> parameter specifies the number of seconds to sleep between querying the queue. This method is most efficient when the command is properly daemonized via <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#supervisor" target="_blank" rel="noopener noreferrer">supervisor</a> or <a href="https://github.com/yiisoft/yii2-queue/blob/master/docs/guide/worker.md#systemd" target="_blank" rel="noopener noreferrer">systemd</a>.</p>
<h2 id="debugging" tabindex="-1"><a class="header-anchor" href="#debugging"><span>Debugging</span></a></h2>
<p>During development you may want to add a panel for the Yii2 debug module. The panel displays a counter and a list of queued tasks.</p>
<p>The <code v-pre>yiisoft/yii2-debug</code> module should be installed in your application for the panel to be displayed.</p>
<p>Configure your application like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'modules'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'debug'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>debug<span class="token punctuation">\</span>Module</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'panels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'queue'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>debug<span class="token punctuation">\</span>Panel</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="gii-code-generator" tabindex="-1"><a class="header-anchor" href="#gii-code-generator"><span>Gii Code generator</span></a></h2>
<p>You can use the Gii code generator to create a job template.</p>
<h3 id="configuration" tabindex="-1"><a class="header-anchor" href="#configuration"><span>Configuration</span></a></h3>
<p>To use the Gii job generator you have to configure it like the following:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token constant">YII_ENV_TEST</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'bootstrap'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'gii'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$config</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'modules'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'gii'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'yii\gii\Module'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'generators'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'job'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>yii<span class="token punctuation">\</span>queue<span class="token punctuation">\</span>gii<span class="token punctuation">\</span>Generator</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>After doing so you’ll find the generator in the Gii menu.</p>
<p><a href="https://cdn.learnku.com/uploads/images/202202/27/93088/64TWfH0RPQ.png!large" target="_blank" rel="noopener noreferrer"><img src="https://cdn.learnku.com/uploads/images/202202/27/93088/64TWfH0RPQ.png!large" alt="default"></a></p>
<h2 id="tests" tabindex="-1"><a class="header-anchor" href="#tests"><span>Tests</span></a></h2>
<h3 id="environment" tabindex="-1"><a class="header-anchor" href="#environment"><span>Environment</span></a></h3>
<p>In order to run tests, you need to install Docker, Docker Compose and the <code v-pre>make</code> utility. Docker configuration files are in <code v-pre>tests/docker</code> and Docker Compose file is <code v-pre>tests/docker-compose.yml</code>. There are configurations for different versions of PHP (5.6, 7.0, 7.1, 7.2, 7.3). You need to create <code v-pre>.env</code> file to specify where the <code v-pre>docker-compose.yml</code> file is. You can create <code v-pre>.env</code> file from <code v-pre>.env.example</code> in the root directory of the project.</p>
<h3 id="running-tests" tabindex="-1"><a class="header-anchor" href="#running-tests"><span>Running Tests</span></a></h3>
<p>To run tests execute the following command:</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># for all PHP versions</span></span>
<span class="line"><span class="token function">make</span> <span class="token builtin class-name">test</span></span>
<span class="line"></span>
<span class="line"><span class="token comment"># for PHP 7.3 only</span></span>
<span class="line"><span class="token function">make</span> test73</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>If you need to pass options to <code v-pre>phpunit</code> use the following commands (for example, to run only one test file):</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">docker-compose</span> build <span class="token parameter variable">--pull</span> php73</span>
<span class="line"><span class="token function">docker-compose</span> run php73 vendor/bin/phpunit tests<span class="token punctuation">\</span><span class="token punctuation">\</span>drivers<span class="token punctuation">\</span><span class="token punctuation">\</span>sqs<span class="token punctuation">\</span><span class="token punctuation">\</span>QueueTest /code/tests/drivers/sqs/QueueTest.php</span>
<span class="line"><span class="token function">docker-compose</span> down</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Some tests can be disabled by default for various reasons (for example, the AWS SQS test require a queue set up in AWS). The test checks <code v-pre>AWS_SQS_ENABLED</code> environment variable (see <code v-pre>\tests\drivers\sqs\QueueTest::setUp</code>). If you want to run that test you need to set this variable to <code v-pre>1</code>. You can specify environment variables that you need to pass to the container in the <code v-pre>.env</code> file in the base directory (see <code v-pre>.env.example</code>). AWS SQS test requires queue credentials that you also need to pass to the container via <code v-pre>.env</code> file (see <code v-pre>tests/app/config/main.php</code>).</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># .env</span></span>
<span class="line"></span>
<span class="line"><span class="token assign-left variable">AWS_SQS_ENABLED</span><span class="token operator">=</span><span class="token number">1</span></span>
<span class="line"><span class="token assign-left variable">AWS_KEY</span><span class="token operator">=</span>KEY</span>
<span class="line"><span class="token assign-left variable">AWS_SECRET</span><span class="token operator">=</span>SECRET</span>
<span class="line"><span class="token assign-left variable">AWS_REGION</span><span class="token operator">=</span>us-east-1</span>
<span class="line"><span class="token assign-left variable">AWS_SQS_URL</span><span class="token operator">=</span>https://sqs.us-east-1.amazonaws.com/234888945020/queue1</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token comment"># AWS SQS test will not be skipped now</span></span>
<span class="line"><span class="token function">make</span> test73</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>💖喜欢本文档的，欢迎点赞、收藏、留言或转发，谢谢支持！<br>
作者邮箱：zhuzixian520@126.com，github地址：<a href="https://github.com/zhuzixian520" target="_blank" rel="noopener noreferrer">github.com/zhuzixian520</a></p>
</blockquote>
</div></template>


